package ru.spb.osll.myfit.userchart;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import ru.spb.osll.myfit.common.util.Pair;
import ru.spb.osll.myfit.util.parser.ILogParser;
import ru.spb.osll.myfit.util.parser.IResultSection;
import ru.spb.osll.myfit.util.parser.ResultSection;

public class ClipsWriterLogParser implements ILogParser {
	private Pattern INFO_LINE = Pattern.compile(".*Writer: clips=(\\d*), rate=(\\d*.\\d*), clipsize=(\\d*).*");
	private  HashMap<String, List<Pair<String, String>>> results = new HashMap<String, List<Pair<String,String>>>();
	//<String,List<Pair<, U>>> 
	
	@Override
	public void infoLine(String line) {
		Matcher matcher = INFO_LINE.matcher(line);
		if(matcher.find()){
			addLine(matcher.group(1), matcher.group(2), matcher.group(3));
		}
	}

	@Override
	public void errorLine(String line) {
	
	}

	@Override
	public List<IResultSection> getResults() {
		List<IResultSection> sec = new LinkedList<IResultSection>();
		for(String clipsSize : results.keySet()){
			ResultSection section = new ResultSection(clipsSize + " B");
			for(Pair<String, String> pair : results.get(clipsSize)){
				section.addRow(pair.getFirst(), pair.getSecond());
			}
			
			sec.add(section);
		}
		return sec;
	}

	@Override
	public void reset() {
	
	}
	
	private void addLine(String clipscount, String rate, String clipsSize){
		if(!results.containsKey(clipsSize)){
			List<Pair<String, String>> res = new LinkedList<Pair<String,String>>();
			results.put(clipsSize, res);
		}
		results.get(clipsSize).add(new Pair<String, String>(clipscount, rate));
	}

}
